trigger deleteRegionList on HCPEducation__c (after update) {
List<CityNameList__c> cnll=new List<CityNameList__c>();
List<RegionNameList__c> rnll=new List<RegionNameList__c>();
List<CityNameList__c> UNcnll=new List<CityNameList__c>();
List<CityNameList__c> UScnll=new List<CityNameList__c>();
List<CityNameList__c> UWcnll=new List<CityNameList__c>();
List<CityNameList__c> UEcnll=new List<CityNameList__c>();
List<CityNameList__c> UDcnll=new List<CityNameList__c>();
List<RegionNameList__c> RDcnll=new List<RegionNameList__c>();

if(System.trigger.isupdate){
 for(HCPEducation__c hcp:trigger.new){
    if((trigger.oldmap.get(hcp.id).NOC__c<>null && trigger.oldmap.get(hcp.id).NOC__c<>0) && 
       (hcp.NOC__c==null || hcp.NOC__c==0)){
      rnll=[select region__c,HCPEducation__c,id from RegionNameList__c where hcpeducation__r.id=:hcp.id and region__r.name=:'华北区'];  
      if(rnll.size()>0){

      cnll=[select id from CityNameList__c where RegionNameList__c =:rnll[0].id];
      
         if(cnll.size()>0){
          for(CityNameList__c cn:cnll){
              UNcnll.add(cn);
            
          }
           delete UNcnll;
         }
         delete rnll;
      }
      
     }
         if((trigger.oldmap.get(hcp.id).SOC__c<>null && trigger.oldmap.get(hcp.id).SOC__c<>0) && 
            (hcp.SOC__c==null || hcp.SOC__c==0)){
      rnll=[select region__c,HCPEducation__c,id from RegionNameList__c where hcpeducation__r.id=:hcp.id and region__r.name=:'华南区'];
     
      if(rnll.size()>0){
         cnll=[select id from CityNameList__c where RegionNameList__c =:rnll[0].id]; 
         if(cnll.size()>0){
          for(CityNameList__c cn:cnll){
              UScnll.add(cn);
          }
          delete UScnll;
         }
         delete rnll;
      }  
      
     }
         if((trigger.oldmap.get(hcp.id).EOC__c<>null && trigger.oldmap.get(hcp.id).EOC__c<>0) &&
            (hcp.EOC__c==null || hcp.EOC__c==0)){
      rnll=[select region__c,HCPEducation__c,id from RegionNameList__c where hcpeducation__r.id=:hcp.id and region__r.name=:'华东区'];
      if(rnll.size()>0){
      cnll=[select id from CityNameList__c where RegionNameList__c=:rnll[0].id];
         if(cnll.size()>0){
          for(CityNameList__c cn:cnll){
              UEcnll.add(cn);
          }
          delete UEcnll;
         }
         delete rnll;
      }
     }
         if((trigger.oldmap.get(hcp.id).WOC__c<>null && trigger.oldmap.get(hcp.id).WOC__c<>0) &&
            (hcp.WOC__c==null || hcp.WOC__c==0)){
      rnll=[select region__c,HCPEducation__c,id from RegionNameList__c where hcpeducation__r.id=:hcp.id and region__r.name=:'华西区'];
      if(rnll.size()>0){
      cnll=[select id from CityNameList__c where RegionNameList__c=:rnll[0].id];
         if(cnll.size()>0){
          for(CityNameList__c cn:cnll){
              UWcnll.add(cn);
          }
          delete UWcnll;
         }
         delete rnll;
      }
     }
    }
}
  /*else if(system.trigger.isdelete){
  
      rnll=[select id from RegionNameList__c where hcpeducation__c=:trigger.old[0].id];
      for(Regionnamelist__c rnl:rnll){
      cnll=[select id from CityNameList__c where RegionNameList__c=:rnl.id];
          for(CityNameList__c cn:cnll){
              UDcnll.add(cn);
          }
       RDcnll.add(rnl);
      }  
      delete UDcnll; 
      delete RDcnll;    
  }*/
}